// pages/users/collect/index.js
const {
  schoolList,
  schoolCollect,
  schoolAdd
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    list: [],
    currentRegion: {
      name: '全国',
      code: 0
    },
    showSelect: false,
    selectData: {
      title: '选择分类',
      key: 'type',
      items: [{
          name: '本科',
          id: 2,
        },
        {
          name: '专科',
          id: 1,
        },
        {
          name: '985',
          id: 'isProject985',
        }, {
          name: '211',
          id: 'isProject211',
        },
      ]
    },
    type: {
      name: '本科',
      id: 2,
    },
    pageNo: 1,
    pageSize: 20,
    setting: {
      width: '690rpx',
      padding: '40rpx 44rpx 34rpx',
      radius: '15rpx'
    },
    loading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pageNo: 1,
      list: []
    })
    this.getList()
    
  },
  async getList() {
    const {
      // type,
      pageNo,
      pageSize
    } = this.data
    const params = {
      pageNo,
      pageSize
    }
    const {
      result
    } = await schoolCollect(params)
    result.records.map((item) => {
      let array = []
      if (item.isProject985 === 1) {
        array.push('985')
      }
      if (item.isProject211 === 1) {
        array.push('211')
      }
      if (item.isDoubleTop === 1) {
        array.push('双一流')
      }
      if (item.isStrongPlan === 1) {
        array.push('强基计划')
      }
      if (item.isDoubleHighPlan === 1) {
        array.push('双高计划')
      }
      item['filter'] = array
    })
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
    })
  },
  changeIndex(e) {

  },
  onReachBottom() {
    const {
      list,
      pageNo,
      pageSize
    } = this.data
    const count = list.length - 1
    if (list[count].length < pageSize) return
    this.getList()
  },

  goto(e) {
    const {
      list
    } = this.data
    const {
      index,
      parent
    } = e.currentTarget.dataset
    const id = list[parent][index].id
    wx.navigateTo({
      url: '/pages/school/detail/index?id=' + id,
    })
  },
  chooseCity() {
    wx.navigateTo({
      url: '/pages/list/index?type=school&category=city',
    })
  },
  chooseType() {
    this.setData({
      showSelect: true
    })
  },
  confirm(e) {
    const {
      key,
      data
    } = e.detail
    this.setData({
      [key]: data,
      pageNo: 1,
      list: []
    })
    this.getList()
  },
  search() {
    wx.navigateTo({
      url: '/pages/search/index?type=school',
    })
  },
  // 院校收藏
  async collect(e) {
    const {
      list,
      pindex,
      index
    } = e.detail
    list[pindex].splice(index, 1)
    this.setData({
      list
    })
    return
    const {
      result
    } = await schoolAdd(id)
    this.data.list[pindex].splice(index, 1)
    wx.showToast({
      title: '取消收藏',
      icon: 'none'
    })
    this.setData({
      list: this.data.list
    })
  },
  // 院校收藏
  collect(e) {
    this.setData({
      list: e.detail.list
    })
  },
})